﻿using Microsoft.Extensions.DependencyInjection;
using Modules.System.Area;
using PmSoft;
using PmSoft.ApplicationContext;
using static Modules.Project.ProjectConstants;

namespace Modules.System.Dept
{
    /// <summary>
    /// 主管单位业务逻辑类
    /// </summary>
    [UseDI(ServiceLifetime.Scoped)]
    public class DeptService
    {
        private readonly SysDeptRepository deptRepository;
        private readonly IApplicationContext applicationContext;

        public DeptService(SysDeptRepository deptRepository, IApplicationContext applicationContext)
        {
            this.deptRepository = deptRepository;
            this.applicationContext = applicationContext;
        }

        /// <summary>
        /// 创建主管单位
        /// </summary>
        /// <param name="dept"></param>
        public void CreateDept(SysDept dept)
        {
            deptRepository.Insert(dept);
        }

        /// <summary>
        /// 全量更新主管单位
        /// </summary>
        /// <param name="dept"></param>
        public void UpdateDept(SysDept dept)
        {
            deptRepository.Update(dept);
        }

        /// <summary>
        /// 删除主管单位
        /// </summary>
        /// <param name="deptId"></param>
        public void DeleteDept(int deptId)
        {
            deptRepository.DeleteByEntityId(deptId);
        }

        /// <summary>
        /// 删除多个主管单位
        /// </summary>
        /// <param name="deptIds"></param>
        public void DeleteDepts(IEnumerable<int> deptIds)
        {
            foreach (int deptId in deptIds)
                DeleteDept(deptId);
        }

        /// <summary>
        /// 获取主管单位信息
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>
        public SysDept? GetSysDept(int deptId)
        {
            return deptRepository.Get(deptId);
        }

        /// <summary>
        /// 主管单位分页
        /// </summary>
        /// <returns></returns>
        public IPagedList<SysDept> GetDepts(DeptQuery query, int pageIndex, int pageSize)
        {
            return deptRepository.GetDepts(query, pageIndex, pageSize);
        }

        /// <summary>
        /// 根据行业分类和区域编码获取主管单位，返回信息包括上级
        /// </summary>
        /// <param name="industryType"></param>
        /// <param name="areaCode"></param>
        /// <returns></returns>
        public IEnumerable<SysDept> GetDeptsByIndustryTypeAndAreaCode(行业主管部门 industryType, int areaCode)
        {
            var areaService = applicationContext.GetService<AreaService>();
            var areas = areaService.GetAreaTuple(areaCode);

            var data = deptRepository.GetDeptsByIndustryType(industryType);
            List<SysDept> depts = new();
            SysDept? sysDept = data.FirstOrDefault(a => a.AreaCode == areaCode && a.Level == AdministrativeLevel.Area);
            if (sysDept != null)
                depts.Add(sysDept);
            sysDept = data.FirstOrDefault(a => a.CityCode == areas.CityCode && a.Level == AdministrativeLevel.City);
            if (sysDept != null)
                depts.Add(sysDept);
            sysDept = data.FirstOrDefault(a => a.ProvinceCode == areas.ProvinceCode && a.Level == AdministrativeLevel.Province);
            if (sysDept != null)
                depts.Add(sysDept);
            return depts;
        }

        /// <summary>
        /// 获取全部部门
        /// </summary>
        /// <returns></returns>
		public IEnumerable<SysDept> GetAllDepts()
        {
            return deptRepository.GetAll();
        }
    }
}